<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            padding: 0;
            margin: 0;
        }

        li {
            list-style: none;
        }

        /* menu */
        .menu {
            width: 100px;
            background-color: rgba(0, 0, 0, 0.1);
            margin: 10px;
        }

        .menu-item {
            position: relative;
            padding: 5px;
            cursor: pointer;
        }

        .menu-content {
            display: none;
            position: absolute;
            left: 100%;
            top: 0;
            width: 200px;
            height: 100px;
            padding: 0 5px;
            background-color: rgba(0, 0, 0, 0.1);
        }

        .menu-item:hover {
            background-color: rgba(0, 0, 0, 0.4);
        }

        .menu-item:hover .menu-content {
            display: block;
        }

        .menu-loading {
            margin: 45px 0 0 92px;
        }
    </style>
</head>

<body>
    <ul id="menu" class="menu"></ul>
    <script type="module">
        import { getJSON } from './ajax/index.js';
        const menu = document.getElementById('menu');
        const url = 'https://www.imooc.com/api/mall-PC/index/menu';
        getJSON(url).then((response) => {
            console.log(response)
            let html='';
            for(const item of response.data){
                html+=`  <li class="menu-item" data-key="${item.key}">
                <span>${item.title}</span>
                <div class="menu-content">
                  <p><img class="menu-loading" src="./loading.gif" alt="加载中" /></p>
                </div>
              </li>
            `;
            }
            menu.innerHTML=html;
        }).then((response)=>{
            const items=menu.querySelectorAll('.menu-item')
            for(const item of items){
                item.addEventListener('mouseenter',()=>{
                    if(item.dataset.done==='done')return;
                    getJSON(`https://www.imooc.com/api/mall-PC/index/menu/${item.dataset.key}`).then(response=>{
                     item.dataset.done='done';
                     let html='';
                     for(const item of response.data){
                        html+=`<p>${item.title}</p>`;
                     }
                     item.querySelector('.menu-content').innerHTML=html;
                    })
                },false)
            }
        })
    </script>
</body>

</html>